﻿Imports Microsoft.VisualBasic
Public Class DateMask
    Inherits DevExpress.XtraEditors.TextEdit
    ''' <summary>
    '''   Cái này dùng để thiết lập EnteMoveNextControl
    ''' Vì bên Form Chứng từ kế toán dùng EnteMoveNextControl, mà Phát hành không dùng cái đó => lởm
    ''' Thiết lập thuộc tính này cần thiết lập trong Design mới được
    ''' </summary>
    ''' <remarks></remarks>
    Private _isMoveNext As Boolean = False
    Public Property isMoveNext() As Boolean
        Get
            Return _isMoveNext
        End Get
        Set(ByVal value As Boolean)
            _isMoveNext = value
        End Set
    End Property

    Private _ReadOnlyDate As Boolean
    Public Property ReadOnlyDate() As Boolean
        Get
            Return _ReadOnlyDate
        End Get
        Set(ByVal value As Boolean)
            _ReadOnlyDate = value
        End Set
    End Property
    Private _EditValue As System.Nullable(Of Date)
    Public Property DateTime() As System.Nullable(Of Date)
        Get
            Return _EditValue
        End Get
        Set(ByVal value As System.Nullable(Of Date))
            OnEditValueChanged(value)
            _EditValue = value
        End Set
    End Property
    Public Overloads Property EditValue() As System.Nullable(Of Date)
        Get
            Return _EditValue
        End Get
        Set(ByVal value As System.Nullable(Of Date))
            OnEditValueChanged(value)
            _EditValue = value
        End Set
    End Property
    Public Overloads Sub OnEditValueChanged(ByVal e As System.Nullable(Of Date))
        If e IsNot Nothing Then
            Me.Text = String.Format("{0:00}/{1:00}/{2:0000}", e.Value.Day, e.Value.Month, e.Value.Year)
        End If
    End Sub
    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        MyBase.OnEnter(e)
        Me.SelectAll()
    End Sub
    Public Sub New()
        InitializeComponent()
        Me.Properties.Mask.EditMask = "(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(1|2)(9|0)[0-9]{2}"
        Me.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx
        Me.Properties.ReadOnly = False
        Me.EnterMoveNextControl = False
    End Sub
    Public Sub New(ByVal _font As System.Drawing.Font)
        InitializeComponent()
        Me.Properties.Mask.EditMask = "(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(1|2)(9|0)[0-9]{2}"
        Me.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx
        Me.Properties.ReadOnly = False
        Me.EnterMoveNextControl = False
        Me.Font = _font
    End Sub
    Private Sub TextEdit1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
        If Me.Text.Trim <> "" Then
            Try
                Dim y
                Try
                    y = CInt(Microsoft.VisualBasic.Right(Me.Text.Trim, 4))
                Catch ex As Exception
                    y = Now.Year
                End Try
                Dim d = CInt(Microsoft.VisualBasic.Left(Me.Text.Trim, 2))
                Dim m = CInt(Me.Text.Trim.Substring(3, 2))
                Dim _date = DateSerial(y, m, d)
                DateTime = _date
            Catch ex As Exception
                Me.Focus()
            End Try
        End If
    End Sub
    
End Class
